import $http from '@/common/http.js'
import $store from '@/store/index.js'

export const getUserMenus = async () => {
  let req = {
    "serviceName": "srvauth_user_app_menu_select",
    "colNames": [
      "*"
    ],
    "condition": [{
      "colName": "client_type",
      "ruleType": "like",
      "value": "一体机"
    }],
    "order": [{
      "colName": "seq",
      "orderType": "asc"
    }]
  }
  const url = `/auth/select/srvauth_user_app_menu_select`
  const res = await $http.post(url, req)
  let resData = res.data.data
  let nos = []
  for (let menu of resData) {
    nos.push(menu.no)
  }
  return await getUserMenusChilds(nos.join(','))
}
export const getUserMenusChilds = async (e) => {
  let rcond = {
    "relation": "OR",
    "data": []
  }
  let nos = e.split(',')
  for (let no of nos) {
    rcond.data.push({
      "relation": "AND",
      "data": [{
          colName: 'path',
          "ruleType": "like",
          "value": '/' + no + '/'
        }
      ]
    })
  }
  let req = {
    "serviceName": "srvconfig_user_menu_select",
    "colNames": [
      "*"
    ],
    page: {
      rownumber: 500,
    },
    "relation_condition": rcond,
    "order": [{
      "colName": "seq",
      "orderType": "asc"
    }]
  }
  const url = `/config/select/srvconfig_user_menu_select`
  const res = await $http.post(url, req)
  const resData = res.data.data
  uni.setStorageSync('user_menu', resData)
  $store.commit('SET_USER_MENU', resData)
  return resData
}
